package com.pwc.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fantuancyh
 * @data 2021/8/21
 */
@Configuration
public class TtlQueueConfig {
    public static final String NORMAL_EXCHANGE = "X";
    public static final String DEAD_LETTER_EXCHANGE = "Y";
    public static final String DELAY_10_QUEUE = "QA";
    public static final String DELAY_40_QUEUE = "QB";
    public static final String DELAY_MANUAL_QUEUE = "QC";
    public static final String DEAD_LETTER_QUEUE = "QD";

    @Bean("exchangeX")
    DirectExchange exchangeX(){
        return new DirectExchange(NORMAL_EXCHANGE,false,false);
    }
    @Bean("exchangeY")
    DirectExchange exchangeY(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE,false,false);
    }

    @Bean("queueA")
    public Queue queueA() {
        Map<String,Object> arg = new HashMap<>();
        arg.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        arg.put("x-dead-letter-routing-key","YD");
        arg.put("x-message-ttl",10000);
        return QueueBuilder.durable(DELAY_10_QUEUE).withArguments(arg).build();
    }
    @Bean("queueB")
    public Queue queueB() {
        Map<String,Object> arg = new HashMap<>();
        arg.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        arg.put("x-dead-letter-routing-key","YD");
        arg.put("x-message-ttl",20000);
        return QueueBuilder.durable(DELAY_40_QUEUE).withArguments(arg).build();
    }
    @Bean("queueC")
    public Queue queueC() {
        Map<String,Object> arg = new HashMap<>();
        arg.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        arg.put("x-dead-letter-routing-key","YD");
        return QueueBuilder.durable(DELAY_MANUAL_QUEUE).withArguments(arg).build();
    }
    @Bean("queueD")
    public Queue queueD() {
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    @Bean
    Binding bindExchangXToQueue10(@Qualifier("exchangeX")Exchange exchange, @Qualifier("queueA")Queue queue){
        return BindingBuilder.bind(queueA()).to(exchangeX()).with("XA");
    }
    @Bean
    Binding bindExchangXToQueue40(@Qualifier("exchangeX")Exchange exchange, @Qualifier("queueB")Queue queue){
        return BindingBuilder.bind(queueB()).to(exchangeX()).with("XB");
    }
    @Bean
    Binding bindExchangXToManualQueue(@Qualifier("exchangeX")Exchange exchange, @Qualifier("queueC")Queue queue){
        return BindingBuilder.bind(queueC()).to(exchangeX()).with("XC");
    }
    @Bean
    Binding bindExchangYToQueueD(@Qualifier("exchangeY")Exchange exchange, @Qualifier("queueD")Queue queue){
        return BindingBuilder.bind(queueD()).to(exchangeY()).with("YD");
    }
}
